package  com.example.cursor_animation


import com.intellij.configurationStore.askToRestart
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.event.EditorFactoryEvent
import com.intellij.openapi.editor.event.EditorFactoryListener
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.HighlighterLayer
import com.intellij.openapi.editor.markup.HighlighterTargetArea
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.startup.StartupActivity
import com.intellij.ui.JBColor
import org.jetbrains.annotations.NotNull
import java.awt.Color
import java.util.Timer
import java.util.TimerTask

class nativeCursorTransition  : ProjectActivity  {
    override suspend fun execute(project: Project) {
        EditorFactory.getInstance().addEditorFactoryListener(object :  EditorFactoryListener {
            override fun editorCreated(event: EditorFactoryEvent) {
                val editor = event.editor
                //println("editor create ")
                if (editor.project == project) {
                    //println("project create ")
                    val caretListener = object : CaretListener {
                        override fun caretPositionChanged(event: CaretEvent) {
                            //println("applyCursorAnimation ")
                            applyCursorAnimation(editor)
                        }
                    }
                    editor.caretModel.addCaretListener(caretListener)
                }
            }
        }, project)
    }

    private fun applyCursorAnimation(editor: Editor) {
        val caretModel = editor.caretModel
        val markupModel = editor.markupModel
        val attributes = TextAttributes()
        attributes.backgroundColor = JBColor.RED
        attributes.effectColor = JBColor.RED
        attributes .effectType=EffectType.ROUNDED_BOX ;
        attributes.fontType = 1
        attributes.foregroundColor = JBColor.RED

//        val highlighter = markupModel.addRangeHighlighter(
//            caretModel.offset,
//            caretModel.offset + 1,
//            HighlighterLayer.CARET_ROW,
//            attributes,
//            HighlighterTargetArea .EXACT_RANGE
//        )

        val timer = Timer()
        timer.schedule(object : TimerTask() {
            private var opacity = 1.0f
            private var increasing = false

            override fun run() {
                if (increasing) {
                    opacity += 0.1f
                    if (opacity >= 1.0f) {
                        increasing = false
                    }
                } else {
                    opacity -= 0.1f
                    if (opacity <= 0.0f) {
                        increasing = true
                    }
                }
                attributes.foregroundColor = Color(1.0f, 0.0f, 0.0f, opacity)
                editor.colorsScheme.setAttributes(TextAttributesKey.createTextAttributesKey("superCursor"), attributes)
                editor.contentComponent.repaint()
            }
        }, 0, 80)
    }
}

